package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.QuestionInfo;
import com.baomidou.ant.service.IQuestionInfoService;
import com.baomidou.ant.utils.VoUtilsTool;
import com.baomidou.ant.vo.Request.*;
import com.baomidou.ant.vo.Response.ResponseGetQuestionVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/question-info")
public class QuestionInfoController {
    @Autowired
    private IQuestionInfoService questionInfoService;

    //新增题目接口
    @PostMapping("/save")
    public Result saveQuestion(@RequestBody RequestSaveQuestionVo requestSaveQuestionVo){
        //判断
        if (VoUtilsTool.checkObjFieldIsNull(requestSaveQuestionVo)){
            return Result.getFailure().setMsg("不能有空项");
        }
        QuestionInfo questionInfo=new QuestionInfo();
        BeanUtils.copyProperties(requestSaveQuestionVo,questionInfo);
        if(questionInfoService.saveOrUpdate(questionInfo)){
            return Result.getSuccess().setMsg("添加成功");
        }else{
            return Result.getFailure().setMsg("添加失败");
        }
    }

    //修改题目信息
    @PostMapping("/updateQuestion")
    public  Result updateQuestion(@RequestBody RequestUpdateQuestionVo requestUpdateQuestionVo) {
        QuestionInfo questionInfo = questionInfoService.getById(requestUpdateQuestionVo.getId());
        //判断空项，保留原值
        if(ObjectUtils.isEmpty(requestUpdateQuestionVo.getAnalysis())){
            requestUpdateQuestionVo.setAnalysis(questionInfo.getAnalysis());
        }
        if(ObjectUtils.isEmpty(requestUpdateQuestionVo.getQuestionLevel())){
            requestUpdateQuestionVo.setQuestionLevel(questionInfo.getQuestionLevel());
        }
        if(ObjectUtils.isEmpty(requestUpdateQuestionVo.getQuestionTitle())){
            requestUpdateQuestionVo.setQuestionTitle(questionInfo.getQuestionTitle());
        }
        if(ObjectUtils.isEmpty(requestUpdateQuestionVo.getQuestionType())){
            requestUpdateQuestionVo.setQuestionType(questionInfo.getQuestionType());
        }
        //修改信息并保存
        BeanUtils.copyProperties(requestUpdateQuestionVo,questionInfo);
        if (questionInfoService.updateById(questionInfo)) {
            return Result.getSuccess().setMsg("修改成功");
        } else {
            return Result.getSuccess().setMsg("修改失败");
        }

    }

    //修改相关答案
    @PostMapping("/updateAnswer")
    public  Result updateAnswer(@RequestBody RequestUpdateAnswerVo requestUpdateAnswerVo) {
        QuestionInfo questionInfo = questionInfoService.getById(requestUpdateAnswerVo.getId());
        //判断空项，保留原值
        if(ObjectUtils.isEmpty(requestUpdateAnswerVo.getAnswer1())){
            requestUpdateAnswerVo.setAnswer1(questionInfo.getAnswer1());
        }
        if(ObjectUtils.isEmpty(requestUpdateAnswerVo.getAnswer2())){
            requestUpdateAnswerVo.setAnswer2(questionInfo.getAnswer2());
        }
        if(ObjectUtils.isEmpty(requestUpdateAnswerVo.getAnswer3())){
            requestUpdateAnswerVo.setAnswer3(questionInfo.getAnswer3());
        }
        if(ObjectUtils.isEmpty(requestUpdateAnswerVo.getAnswer4())){
            requestUpdateAnswerVo.setAnswer4(questionInfo.getAnswer4());
        }
        //修改信息并保存
        BeanUtils.copyProperties(requestUpdateAnswerVo,questionInfo);
        if (questionInfoService.updateById(questionInfo)) {
            return Result.getSuccess().setMsg("答案信息修改成功");
        } else {
            return Result.getSuccess().setMsg("出问题了，修改失败");
        }

    }

    @GetMapping("/getById/{questionId}")
    public Result getById(@PathVariable Integer questionId) {
        QuestionInfo questionInfo = questionInfoService.getById(questionId);
        ResponseGetQuestionVo vo=new ResponseGetQuestionVo();
        BeanUtils.copyProperties(questionInfo,vo);
        return Result.getSuccess().setData(vo);
    }

    //题库查询接口
    @PostMapping("/questionList")
    public  Result questionList(@RequestBody RequestQuestionListVo requestQuestionListVo){
        IPage<QuestionInfo> iPage=questionInfoService.questionList(requestQuestionListVo);
        return Result.getSuccess().setData(iPage);
    }

    //通过id删除
    @DeleteMapping("/delete/{questionId}")
    public Result delete(@PathVariable Integer questionId){
        if(questionInfoService.removeById(questionId)){
            return Result.getSuccess().setMsg("删除成功!");
        }else {
            return Result.getFailure().setMsg("删除失败!");
        }
    }

    //批量
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody RequestDeleteVo requestDeleteVo){
        List<Integer> ids= Arrays.asList(requestDeleteVo.getIds());
        if(ids.size()>0&&questionInfoService.removeByIds(ids)){
            return Result.getSuccess().setMsg("删除成功");
        }else{
            return Result.getFailure().setMsg("删除失败");
        }
    }
}
